package com.meizhuang.controller;



import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.meizhuang.entity.*;
import com.meizhuang.mapper.base.BaseWrapper;
import com.meizhuang.param.request.PageRequest;
import com.meizhuang.result.PageResult;
import com.meizhuang.service.ComplaintService;
import com.meizhuang.service.LeaveWordService;
import com.meizhuang.service.UserInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


import com.meizhuang.aspect.SystemOperationLog;
import com.meizhuang.base.BaseController;
import com.meizhuang.entity.enums.SystemOperationType;
import com.meizhuang.result.JsonResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.stream.Collectors;

@ApiIgnore
@RestController
public class IndexController extends BaseController {

	@Autowired
	private LeaveWordService leaveWordService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private ComplaintService complaintService;

	@RequestMapping(value = "/index")
	public ModelAndView index() {
		return new ModelAndView("index/index");
	}

	@RequestMapping(value = "/indexdefault")
	public ModelAndView indexdefault() {
		return new ModelAndView("index/indexdefault");
	}

	@RequestMapping(value = "/power")
	public ModelAndView power() {
		return new ModelAndView("error/power");
	}

	// 用户退出
	@RequestMapping(value = "/logout")
	@SystemOperationLog(value = "退出登录", operationType = SystemOperationType.UPDATE)
	public JsonResult<Object> logout() {
		super.getSession().invalidate();
		return JsonResult.buildSuccess("操作成功");
	}

	@RequestMapping(value = "/initDate")
	public JsonResult<String> initDate() {
		
		return JsonResult.buildSuccess("操作成功");
	}
	
	/**
	 * 刷新资源权限
	 * 
	@RequestMapping(value = "/refreshResourceAuthority")
	@SystemOperationLog(value = "刷新资源权限", operationType = SystemOperationType.UPDATE)
	public JsonResult<Object> refreshResourceAuthority() {
		AdminSession adminSession = (AdminSession) super.getSession().getAttribute(AdminSession.SYSTEMUSER_SYSTEM_SESSIONID);
		Authority.clearCache(String.valueOf(adminSession.getId()));
		List<Menu> menus = Authority.findMenuLists(adminSession.getId());
		
		
		
		for (Menu menu : menus) {
			if (StringUtils.isEmpty(menu.getIcon())) {
				menu.setIcon(IconClass.getIcon());
			}
		}
		super.getSession().setAttribute("menus", menus);
		return JsonResult.buildSuccess("刷新成功");
	}*/
	//留言
	@RequestMapping(value = "/leaveMessage")
	public PageResult<LeaveWordExt> leaveMessage(PageRequest pageRequest) {

		Page<LeaveWord> page = new Page(pageRequest.getPage(), pageRequest.getLimit(), pageRequest.getSortField(), pageRequest.getAscState());

		leaveWordService.selectPage(page);

		List<LeaveWord> leaveWords = page.getRecords();

		if(CollectionUtils.isEmpty(leaveWords)){
			return PageResult.buildSuccess(page.getTotal(), new ArrayList<>());
		}

		EntityWrapper<UserInfo> entityWrapper = new EntityWrapper<UserInfo>();
		entityWrapper.in("uid", leaveWords.stream().map(m->m.getFormUid()).distinct().collect(Collectors.toList()));

		List<UserInfo> userInfoList = userInfoService.selectList(entityWrapper);


		List<LeaveWordExt> collect = leaveWords.stream().map(LeaveWordExt::new).filter(leaveWordExt -> Objects.nonNull(leaveWordExt.getFormUid())).peek(c -> {

			userInfoList.stream().filter(f->f.getUid().intValue() == c.getFormUid().intValue()).findFirst().ifPresent(p->{
				c.setStoreName(p.getStoreName());
			});
		}).collect(Collectors.toList());


		return PageResult.buildSuccess(page.getTotal(), collect);
	}


	//留言 删除
	@RequestMapping(value = "/leaveMessage_delete")
	public JsonResult leaveMessage_delete(List<Integer> ids) {

		Map<String,Object> retMap = new LinkedHashMap<>();
		leaveWordService.deleteBatchIds(ids);
		return JsonResult.buildSuccess(retMap);
	}



	//投诉
	@RequestMapping(value = "/complaint")
	public PageResult<ComplaintExt> complaint(PageRequest pageRequest) {

		Page<Complaint> page = new Page(pageRequest.getPage(), pageRequest.getLimit(), pageRequest.getSortField(), pageRequest.getAscState());
		complaintService.selectPage(page);

		List<Complaint> complaintList = page.getRecords();

		if(CollectionUtils.isEmpty(complaintList)){
			return PageResult.buildSuccess(page.getTotal(), new ArrayList<>());
		}

		EntityWrapper<UserInfo> entityWrapper = new EntityWrapper<UserInfo>();
		entityWrapper.in("uid", complaintList.stream().map(m->m.getFormUid()).distinct().collect(Collectors.toList()));

		List<UserInfo> userInfoList = userInfoService.selectList(entityWrapper);


		List<ComplaintExt> collect = complaintList.stream().map(ComplaintExt::new).peek(c -> {

			userInfoList.stream().filter(f->f.getUid().intValue() == c.getFormUid().intValue()).findFirst().ifPresent(p->{
				c.setStoreName(p.getStoreName());
			});
		}).collect(Collectors.toList());

		return PageResult.buildSuccess(page.getTotal(), collect);
	}
}
